

import ij.*;
import ij.process.*;
import ij.gui.*;
import java.awt.*;
import java.util.*;
import ij.plugin.*;
import java.io.*;
import java.util.*;
import java.lang.*;
import ij.plugin.frame.*;
import javax.swing.*;
import java.awt.event.*;



public class Contrast_pattern_reduced_ extends PlugInFrame implements ActionListener 
{
	
	MersenneTwister mt_letters,mt_blokjes;
	int w = 1024;
	int h = 768;
	int nchar=10;
	int imax=255;
	int depth=8;
	int stepfront=1;
		
		
	String combina;
	
	String letterset="ABCDEHJKLMPSTUZ";
	int seed;
	boolean bars;
	JButton bCrea;
	JSlider slStep,slNChar;
	ButtonGroup bgRes;
	JRadioButton rbRes1,rbRes2,rbRes3;
	ButtonGroup bgBit;
	JRadioButton rbBit1,rbBit2,rbBit3,rbBars;
	JTextField tfSeed;
	
	public Contrast_pattern_reduced_() 
	{
		super("Contrast pattern creator");
		setLayout(new GridLayout(5,3));
		
		setBackground(SystemColor.control);
		
		bgRes=new ButtonGroup();
		
		rbRes1=new JRadioButton ("640*480",false);
		rbRes2=new JRadioButton ("1024*768",true);
		rbRes3=new JRadioButton ("1280*1024",false);
		
		
		bgRes.add(rbRes1);
		bgRes.add(rbRes2);
		bgRes.add(rbRes3);
		
		
		add(rbRes1);
		add(rbRes2);
		add(rbRes3);
		
		bgBit=new ButtonGroup();
		
		rbBit1=new JRadioButton ("8-bit",true);
		rbBit2=new JRadioButton ("10-bit",false);
		rbBit3=new JRadioButton ("12-bit",false);
		
		bgBit.add(rbBit1);
		bgBit.add(rbBit2);
		bgBit.add(rbBit3);
		
		add(rbBit1);		
		add(rbBit2);		
		add(rbBit3);		
		
		JLabel lSeed=new JLabel("random seed");
		add(lSeed);
		
		Calendar cal = Calendar.getInstance(TimeZone.getDefault());
		
		seed=cal.get(Calendar.YEAR)*10000+(cal.get(Calendar.MONTH)+1)*100+cal.get(Calendar.DAY_OF_MONTH);
		tfSeed = new JTextField(Integer.toString(seed),8);
		add(tfSeed);

		rbBars=new JRadioButton ("include bar patterns", true);		
		add(rbBars);			
				
		JLabel lStep=new JLabel("contrast step size");
		add(lStep);
					
		slStep=new JSlider(1,20,1);
		slStep.setMajorTickSpacing(4);
		slStep.setMinorTickSpacing(1);
		slStep.setPaintTicks(true);
		slStep.setPaintLabels(true);
		add(slStep);	
		
		JLabel lEmpty2=new JLabel("");
		add(lEmpty2);
		
		JLabel lNChar=new JLabel("number of characters");
		add(lNChar);
					
		slNChar=new JSlider(10,30,10);
		slNChar.setMajorTickSpacing(4);
		slNChar.setMinorTickSpacing(1);
		slNChar.setPaintTicks(true);
		slNChar.setPaintLabels(true);
		add(slNChar);	
		
		bCrea = new JButton("create image");
		bCrea.addActionListener(this);
		add(bCrea);
		
		pack();
		GUI.center(this);
		show();
	}
	
	public void actionPerformed(ActionEvent e) 
	{
		if (e.getSource() == bCrea) 	
		{
			if (rbRes1.isSelected())
			{
				w=640; 	h=480;
			}
			if (rbRes2.isSelected())
			{
				w=1024; h=768;
			}
			if (rbRes3.isSelected())
			{
				w=1280; h=1024;
			}
			
			if (rbBit1.isSelected())
				depth=8;
			
			if (rbBit2.isSelected())
				depth=10;
			
			if (rbBit3.isSelected())
				depth=12;
			
			
			stepfront=slStep.getValue();
			
			nchar=slNChar.getValue();
			
			seed=Integer.parseInt(tfSeed.getText()); 
	
			mt_letters = new MersenneTwister();
			mt_blokjes = new MersenneTwister();
			
			mt_letters.sgenrand(seed);
			mt_blokjes.sgenrand(seed);
			
			if (rbBars.isSelected())
				bars=true;
				
			crea_ima();
		}
		
		
	}
	
	void barvol(ImageProcessor ip,int x1,int y1,int x2,int y2,int d)
	{
		int i,j;
		for(j=y1;j<y2;j++)
			for(i=x1;i<x2;i++)
			{
				ip.putPixelValue(i,j,ip.getPixelValue(i,j)+d);
			}
		return;	
	}
	
	void barhor(ImageProcessor ip,int x1,int y1,int x2,int y2,int d,int r)
	{
		int i,j;
		for(j=y1;j<y2;j++)
			if ((j%r)<(r/2))
			for(i=x1;i<x2;i++)
			{
				ip.putPixelValue(i,j,ip.getPixelValue(i,j)+d);
			}
		return;	
	}

	void barvert(ImageProcessor ip,int x1,int y1,int x2,int y2,int d,int r)
	{
		int i,j;
		for(j=x1;j<x2;j++)
			if ((j%r)<(r/2))
			for(i=y1;i<y2;i++)
			{
				ip.putPixelValue(j,i,ip.getPixelValue(j,i)+d);
			}
		return;	
	}

	public void crea_bars(ImageProcessor ip,int x,int y,int d)
	{
		int i,j,k,xfin;
		int[]oriset={0,1,2,3,4,0,6};
		int[]finset={0,0,0,0,0,0,0};
		
		oriset[6]=Math.abs(mt_blokjes.genrand()%5);
		
		
		for(i=0;i<7;i++)
		{
        		k=mt_blokjes.genrand();
        		k=Math.abs(k%(7-i));
        		finset[i]=oriset[k];
        		for(j=k;j<6;j++)
        	        	oriset[j]=oriset[j+1];
   		}
    		
    		for(i=0;i<7;i++)
    		{
    			xfin=x-85+i*30;
    			if ((finset[i]==0))
    			{	
    				barvol(ip,xfin,y-10,xfin+20,y+10,d);		
    				combina+="o";
    			}
    			else if(finset[i]==1)
    			{
    				barvert(ip,xfin,y-10,xfin+20,y+10,d*2,2);	
    				combina+="v";
    			}
    			else if(finset[i]==2)
    			{
    				barhor(ip,xfin,y-10,xfin+20,y+10,d*2,2);		
    				combina+="h";
    			}
    			else if(finset[i]==3)
    			{
    				barvert(ip,xfin,y-10,xfin+20,y+10,d*2,4);
    				combina+="V";
    			}
    			else if(finset[i]==4)
    			{
    				barhor(ip,xfin,y-10,xfin+20,y+10,d*2,4);
    				combina+="H";
    			}
    		}
		
	}
	
	
	
	public void crea_ima()
	{
		ImagePlus imp;
		ImageProcessor ip;
		Roi r;
		char letter;
	        int back,dir,offsetx,offsetbarsx,offsety,zone,i;
	        int lwidth=20;
	        int hfract=30;
	       	
	       	if (depth==8)
		{	ip=new ByteProcessor(w,h);	imax=255;
			
		}
			
		else
		{	ip=new ShortProcessor(w,h);	
			if (depth==10) imax=1023;
			if (depth==12) imax=4095;
		}
		
		imp=new ImagePlus("contrast_"+Integer.toString(seed),ip);
        	imp.show(); 
        	ip.setFont(new Font(null,Font.BOLD,20));
	        
	        
	        

// initialiseer randomgenerator op basis van datum
// "eigen" random generator gekozen om getallen te kunnen genereren onder ander applicatie 
	        
		offsetx=(w-(nchar+1)*lwidth)/2*(seed%3);
		offsetbarsx=w/6*(((seed+1)%3)*2+1);
	        
	        
	        IJ.write("contrast pattern generated with seed="+seed);
	        
		for(zone=0;zone<4;zone++)
	      	{  
	        	
	        	offsety=(2*zone+1)*h/8;
	        	back=(seed+zone-1)%4*imax/3;
		        if (back<imax/2) 
		        	dir=stepfront;
	        	else 
	        		dir=-stepfront;
	        
	        	combina=new String("zone"+zone+":\t"+back+"\t");
	        	
	        	
			for(i=0;i<w;i++)
	        		for(int j=zone*h/4;j<(zone+1)*h/4;j++)
		        		ip.putPixelValue(i,j,back);
		
	        	for(i=nchar;i>0;i--)
	        	{
	        		ip.setValue((double)(back+i*dir));
	        		letter = letterset.charAt((int)(((long)mt_letters.genrand() & 0xffffffffL) % letterset.length()));
	        		combina+=letter;
        			String text = " "+letter;
        			ip.moveTo(offsetx+lwidth*(10-i), offsety+9);
        			
        			ip.drawString(text);
        		}
        		combina+="\t";
        		if (bars==true)
        		{
        			crea_bars(ip,offsetbarsx,offsety,nchar*dir);
        		}
        		
        		IJ.write(combina);
              	}
        	imp.updateAndDraw();
	}

}